package com.sinoglobal.sinostore.adapter;

import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;

import com.lidroid.xutils.util.LogUtils;
import com.sinoglobal.sinostore.bean.ChildActivityList.ChildActivityVo;
import com.sinoglobal.sinostore.fragment.SecondKillFragment;
import com.sinoglobal.sinostore.utils.DateUtils;

/**
 * @author sbk
 * @createdate 2014-7-3 下午4:19:46
 * @Description: 首页适配器
 */
public class SecondKillFragmentPagerAdapter extends FragmentStatePagerAdapter {

	private FragmentManager fm;
	private ArrayList<ChildActivityVo> list;
	private Long serverTime;

	public SecondKillFragmentPagerAdapter(FragmentManager fm) {
		super(fm);
		list = new ArrayList<ChildActivityVo>();
	}

	public SecondKillFragmentPagerAdapter(FragmentManager fm, ArrayList<ChildActivityVo> list,Long serverTime) {
		super(fm);
		this.fm = fm;
		this.list = list;
		this.serverTime = serverTime;
	}

	public ArrayList<ChildActivityVo> getList() {
		return list;
	}

	public void setList(ArrayList<ChildActivityVo> list,Long serverTime) {
		this.list.addAll(list);
		this.serverTime = serverTime;
		notifyDataSetChanged();
	}

	@Override
	public int getItemPosition(Object object) {
		return PagerAdapter.POSITION_NONE;
	}

	@Override
	public Fragment getItem(int position) {
		if (null == list.get(position).fragment) {
			Fragment f = SecondKillFragment.newInstance(position, list.get(position).id);
			list.get(position).fragment = f;
		}
		return list.get(position).fragment;
	}

	@Override
	public int getCount() {
		if (null == list) {
			return 0;
		}
		return list.size();
	}
	
	public CharSequence getPageTitle(int position) {
		Long startTime = Long.parseLong(list.get(position).start_time);
		Long time = startTime - serverTime;
		String showTime;
		if(Math.abs(time)>86400){
			showTime = DateUtils.parseTimeStampToDateString(Long.parseLong(list.get(position).start_time));
		}else{
			showTime = DateUtils.parseTimeStampToHHMMSSString(Long.parseLong(list.get(position).start_time));
		}
		String result = list.get(position).name+"\n"+showTime;
		return result;
	}

}
